package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.S_UNION_CHANGE_RANDOM_JOIN, desc = "修改随机加入")
public class ChangeRandomJoinCmd extends AbstractCommand {

    private boolean isAllowRandomJoin;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionProto.UnionChangeRandomJoinReqMsg reqMsg = UnionProto.UnionChangeRandomJoinReqMsg.parseFrom(packet.getBytes());
        isAllowRandomJoin = reqMsg.getIsAllowRandomJoin();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 判断权限 族长 副族长才有权限
        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionMember member = CrossUnionMgr.getUnionMember(unionUid, player.getUserId());
        if(null == member){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        player.sendPacket(Protocol.C_UNION_CHANGE_RANDOM_JOIN, UnionProto.UnionChangeRandomJoinReqMsg.newBuilder().setIsAllowRandomJoin(isAllowRandomJoin));
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0) {
            UnionProto.UnionChangeRandomJoinRespMsg.Builder respMsg = UnionProto.UnionChangeRandomJoinRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_CHANGE_RANDOM_JOIN, respMsg);
        }
    }
}
